import count from '@dfeidao/atom-nodejs/db/count';
import query from '@dfeidao/atom-nodejs/db/query';
import sql_query from '@dfeidao/atom-nodejs/db/sql-query';
import end_day from '@dfeidao/atom-nodejs/dt/end-day';
import end_year from '@dfeidao/atom-nodejs/dt/end-year';
import format from '@dfeidao/atom-nodejs/dt/format';
import parse from '@dfeidao/atom-nodejs/dt/parse';
import start_day from '@dfeidao/atom-nodejs/dt/start-day';
import start_year from '@dfeidao/atom-nodejs/dt/start-year';
import sub_days from '@dfeidao/atom-nodejs/dt/sub-days';
import sub_year from '@dfeidao/atom-nodejs/dt/sub-year';
import sys_date from '@dfeidao/atom-nodejs/msg/sys-date';
import { systemid } from '../../atom/config';
interface Message {
	filter: {
		productid: string
	};
}
interface Data {
	create_time: number;
	create_times: string;
}


export default async function atom(msg: Message, action_id: string, session_id: string) {
	// 查询数据
	const { prepare: p, exec: e } = count(action_id, session_id, systemid);
	const { prepare, exec } = query(action_id, session_id, systemid);
	const { prepare: p1, exec: e1 } = query(action_id, session_id, systemid);
	prepare('project_type', ['type_name'], msg.filter, 200, 1, [], []);
	const [data] = await exec();// 数据
	const type_names = new Array();
	data.forEach((v: { type_name: string; }) => {
		type_names.push(v.type_name);
	});
	p1('company_project', ['project_no', 'type_name'], { type_name: { $in: type_names } }, 200, 1, [], []);
	// 项目信息
	p('company_project', { type_name: { $in: type_names } });
	const [c] = await e();
	const [data1] = await e1();
	const t = {};
	data1.forEach((v: { type_name: string; project_no: string }) => {
		const type_name = v.type_name;
		if (type_name in t) {
			t[type_name].push(v.project_no);
		} else {
			t[type_name] = [];
			t[type_name].push(v.project_no);
		}
	});
	// 项目风险
	const { prepare: p2, exec: e2 } = query(action_id, session_id, systemid);
	p2('risk_management', ['risk_no', 'create_time', 'risk_content'], {}, 3, 1, [], ['create_time']);
	const [data2] = await e2<Data>();
	data2.forEach((v) => {
		v.create_times = format(v.create_time, 'YYYY-MM-DD ');
	});
	// // 项目统计
	// // const { prepare: p3, exec: e3 } = query(action_id, session_id, systemid);
	// // p3('dept', ['deptname'], {}, 200, 1, [], []);
	// // const [data3] = await e3();
	// // const deptnames = new Array();
	// // data3.forEach((v: { deptname: string; }) => {
	// // 	deptnames.push(v.deptname);
	// // });
	// // const { prepare: p4, exec: e4 } = query(action_id, session_id, systemid);
	// // p4('company_project', ['deptname', 'project_no', 'contract_amount'], { deptname: { $in: { deptnames } } }, 200, 1, [], ['create_time']);
	// // const [data4] = await e4();
	// const query1 = cq(action_id, session_id, systemid);
	// const data3 = await query1
	// 	.add_field('dept', 'deptname', 'deptname')
	// 	.add_fun('company_project', 'contract_amount', 'contract_amount', 'sum')
	// 	.add_fun('company_project', 'project_no', 'project_no', 'count')
	// 	.inner_join('dept', 'company_project', ['deptname', 'equip_depart_name'])
	// 	.groupby('dept', 'deptname')
	// 	.exec();

	// 时间
	const startdday = start_day(sys_date(action_id, session_id));// 今天的开始时间
	const endday = end_day(sys_date(action_id, session_id));// 今天
	const startday = start_day(sub_days(endday, 6).getTime());// 一周
	const starttday = start_day(sub_days(endday, 29).getTime());// 一月
	const endyear = end_year(sys_date(action_id, session_id));// 今年
	const startyear = start_year(sub_year(endyear, 1).getTime());// 一年
	// 工单统计图
	const count0 = await sql_query<{ create_time: string }>(
		action_id,
		session_id,
		systemid,
		msg.filter.productid,
		"select from_unixtime((create_time/1000),'%Y-%m-%d') create_time,count(create_time) count from work_order GROUP BY from_unixtime((create_time/1000),'%Y-%m-%d')",
		'postgre');
	let create_tim;
	const count1 = count0.filter((v) => {
		create_tim = parse(v.create_time);
		return create_tim >= startday;
	});

	return { dataArray1: t, count: c, dataArray2: data2, count1, startday, endday, startyear, endyear, startdday, starttday };
}
